package com.turnengine.client.local.unit.command.gwt;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.javabi.codebuilder.generated.gwt.IGeneratedRemoteServiceServlet;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.gwt.GwtErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.unit.bean.IUnit;
import com.turnengine.client.local.unit.command.AddUnit;
import com.turnengine.client.local.unit.enums.UnitMove;
import com.turnengine.client.local.unit.enums.UnitNode;
import com.turnengine.client.local.unit.enums.UnitType;

/**
 * The Add Unit Servlet.
 */
public class AddUnitServlet extends RemoteServiceServlet implements IGeneratedRemoteServiceServlet, AddUnitService {

	public IUnit addUnit(long loginId, int instanceId, String name, UnitNode node, UnitType type, UnitMove move, String groupName, String factionName) throws GwtErrorCodeException {
		AddUnit command = new AddUnit(loginId, instanceId, name, node, type, move, groupName, factionName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUnit> response = service.execute(command);
		if (response.hasErrors()) {
			throw new GwtErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
